/*
 * BorderScrollView.fx
 *
 * Created on 04.12.2009, 14:08:32
 */

package org.zhgames.zhciv.client.gui;

import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.control.ScrollBar;
import javafx.util.Math;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.*;
import javafx.scene.input.MouseEvent;
import javafx.animation.*;

/**
 * scroll node on mouse moved on border
 * @author Vitaliy_Turilkin
 */

public class BorderScrollView extends CustomNode{
    public-init var node: Node;
    public var width: Float;
    public var height: Float;
    public var fill: Paint;

    var group: Group;
    var hgap = bind group.layoutBounds.height - height;
    var wgap = bind group.layoutBounds.width - width;

    def vscrollBar: ScrollBar = ScrollBar {
        translateX: bind width - vscrollBar.width
        min: 0
        max: bind Math.max(0, hgap)
        visible: false
        vertical: true
        height: bind height
    }
    def hscrollBar: ScrollBar = ScrollBar {
        translateX: bind width - hscrollBar.width
        min: 0
        max: bind Math.max(0, wgap)
        visible: false
        vertical: false
        height: bind height
    }

    override function create() {
        Group {
            content: [
                Rectangle {//left
                    x:0
                    y:0
                    width:2
                    height: bind height
                    onMouseEntered: function( e: MouseEvent ):Void {
                        left_timeline.playFromStart();
                    }
                    onMouseExited: function( e: MouseEvent ):Void {
                        left_timeline.stop();
                    }
                },
                Rectangle {//up
                    x:0
                    y:0
                    width:bind width
                    height: 2
                    onMouseEntered: function( e: MouseEvent ):Void {
                        up_timeline.playFromStart();
                    }
                    onMouseExited: function( e: MouseEvent ):Void {
                        up_timeline.stop();
                    }
                },
                Rectangle {//down
                    x:0
                    y:bind height - 2
                    width:bind width
                    height: 2
                    onMouseEntered: function( e: MouseEvent ):Void {
                        down_timeline.playFromStart();
                    }
                    onMouseExited: function( e: MouseEvent ):Void {
                        down_timeline.stop();
                    }
                },
                Rectangle {//right
                    x:bind width - 2
                    y:0
                    width:2
                    height: bind height
                    onMouseEntered: function( e: MouseEvent ):Void {
                        right_timeline.playFromStart();
                    }
                    onMouseExited: function( e: MouseEvent ):Void {
                        right_timeline.stop();
                    }
                },
                Rectangle {
                    fill: bind fill
                    width: bind width
                    height: bind height
                },
                Group {
                    content: [
                        group = Group {
                            translateY: bind - vscrollBar.value
                            translateX: bind - hscrollBar.value
                            content: [node]
                        }
                    ]
                    clip: Rectangle {
                        width: bind width
                        height: bind height
                    }
                },
                vscrollBar,
                hscrollBar
            ]
        }
    }

    def left_timeline = Timeline {
        repeatCount: Timeline.INDEFINITE
        keyFrames: [
            at (0s) {hscrollBar.value => hscrollBar.value},  //start point
            at (5s) {hscrollBar.value => hscrollBar.value - 50 tween Interpolator.EASEIN} //end point
        ];
     }
     def right_timeline = Timeline {
        repeatCount: Timeline.INDEFINITE
        keyFrames: [
            at (0s) {hscrollBar.value => hscrollBar.value},  //start point
            at (5s) {hscrollBar.value => hscrollBar.value + 50 tween Interpolator.EASEIN} //end point
        ]; 
     }
     def up_timeline = Timeline {
        repeatCount: Timeline.INDEFINITE
        keyFrames: [
            at (0s) {vscrollBar.value => vscrollBar.value},  //start point
            at (5s) {vscrollBar.value => vscrollBar.value - 50 tween Interpolator.EASEIN}
        ];
     }
     def down_timeline = Timeline {
        repeatCount: Timeline.INDEFINITE
        keyFrames: [
            at (0s) {vscrollBar.value => vscrollBar.value},  //start point
            at (5s) {vscrollBar.value => vscrollBar.value + 50 tween Interpolator.EASEIN}
        ];
     }
}
